﻿using Pay.Core;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Pay.Token.Controllers {
    [AppAuthorize("Member")]
    public class TaskController : GeneralController {
        private ISoupTaskService _iSoupTaskService;
        private readonly IRedRecordService _iRedRecordService;
        private IAdvertisementService _iAdvertisementService;
        private ISettingService _settingService;
        private IMerchantService _iMerchantService;
        public TaskController(ISoupTaskService iSoupTaskService, IRedRecordService redRecordService, IAdvertisementService advertisementService, ISettingService settingService, IMerchantService iMerchantService) {
            _iSoupTaskService = iSoupTaskService;
            _iRedRecordService = redRecordService;
            _iAdvertisementService = advertisementService;
            _settingService = settingService;
            _iMerchantService = iMerchantService;
        }
        [AllowAnonymous]
        public ActionResult List() {
            ViewData["list"] = _iSoupTaskService.GetTaskList(UserId);
            merch_advertisement_query_type query = new merch_advertisement_query_type();
            query.position_type = (int)EAdvertisementPositionType.TaskListTop;
            ViewData["ad"] = (_iAdvertisementService.AppGetAdvertisementList(query) ?? new List<ad_dto>()).FirstOrDefault();
            ViewData["site_img"] = RequestMethod.token_imgurl;
            if (UserId > 0) {
                var member = _iMerchantService.Get(UserId);
                ViewBag.mayi = t_RequestMethod.mayi + "?m=" + member.merchant_no;
            } else ViewBag.mayi = t_RequestMethod.mayi;
            return View();
        }
        /// <summary>
        /// 获得任务详情（记录点击数）
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetSoupTask(long taskId) {
            var result = _iSoupTaskService.AppGetSoupTask(taskId, UserId);
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }
        #region 币金所
        public ActionResult CoinGold() {
            var model = _iSoupTaskService.AppGetSoupTask(0, UserId, TO.ETaskType.CoinGold);
            ViewData["CoinGold"] = model;
            return View();
        }
        public async Task<JsonResult> CoinGoldPost() {
            var result = await _iSoupTaskService.TaskRecive(UserId, TO.ETaskType.CoinGold);
            if (result) {
                return Json(ResultToJson.Json("领取成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("您还没有达到任务要求，无法完成任务", false), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 时代云商
        public ActionResult ShiDaiYunShang() {
            var model = _iSoupTaskService.AppGetSoupTask(0, UserId, TO.ETaskType.ShiDaiYunShang);
            ViewData["CoinGold"] = model;
            return View();
        }
        public async Task<JsonResult> ShiDaiYunShangPost() {

            var result = await _iSoupTaskService.TaskRecive(UserId, TO.ETaskType.ShiDaiYunShang);
            if (result) {
                return Json(ResultToJson.Json("领取成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("您还没有达到任务要求，无法完成任务", false), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 戈仑埠
        public ActionResult GeLunBu() {
            var model = _iSoupTaskService.AppGetSoupTask(0, UserId, TO.ETaskType.GeLunBu);
            ViewData["CoinGold"] = model;
            return View();
        }
        public async Task<JsonResult> GeLunBuPost() {
            var result = await _iSoupTaskService.TaskRecive(UserId, TO.ETaskType.GeLunBu);
            if (result) {
                return Json(ResultToJson.Json("领取成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("您还没有达到任务要求，无法完成任务" +
                    "" +
                    "" +
                    "", false), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 创客奖励任务
        public ActionResult Maker() {
            var model = _iSoupTaskService.AppGetSoupTask(0, UserId, TO.ETaskType.Maker);
            ViewData["Maker"] = model;
            return View();
        }
        public async Task<JsonResult> MakerPost() {
            var result = await _iSoupTaskService.TaskRecive(UserId, TO.ETaskType.Maker);
            if (result) {
                return Json(ResultToJson.Json("领取成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("您还没有达到任务要求，无法完成任务" +
                    "" +
                    "" +
                    "", false), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion

        #region 邀请好友注册
        public ActionResult InviteRegister() {
            var model = _iRedRecordService.GetUser(UserId);
            ViewBag.url = t_RequestMethod.invitation_register_url + "?m=" + UserId;
            ViewData["reduser"] = model;
            return View();
        }
        #endregion

        #region 实名认证
        /// <summary>
        /// 实名认证
        /// </summary>
        /// <returns></returns>
        public ActionResult Authentication() {
            var user = _iMerchantService.GetAuth(UserId);
            if (user == null) user = new authinfo_dto { certificate_type = 1 };
            return View(user);
        }

        /// <summary>
        /// 实名信息完善信息（上传证件照）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult AuthenticationPerfect(token_auth_model model) {
            ViewBag.AuthModel = model;
            ViewBag.ImgUrl = RequestMethod.token_imgurl;
            return View();
        }

        /// <summary>
        /// 实名认证提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> AuthenticationPost(token_auth_model model) {
            if (!ModelState.IsValid)
                return Json(ResultToJson.Json<bool>(ModelError));
            string path = base.Upload("authentication", "pz1");//如果有进行重新拍照，取重新拍照的file
            if (string.IsNullOrEmpty(path))
                path = base.Upload("authentication", "pz");
            if (!string.IsNullOrEmpty(path)) {
                model.card_img = path;
            }
            if (string.IsNullOrEmpty(model.card_img)) {
                throw new ServerErrorException("请上传手持身份证照片");
            }
            var result = await _iMerchantService.VerificationCard(model, UserId);
            if (result != null) {
                return Json(ResultToJson.Json("实名认证成功", result), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json<bool>("实名认证失败"), JsonRequestBehavior.AllowGet);
            }
        }
        /// <summary>
        /// 实名认证成功
        /// </summary>
        /// <returns></returns>
        public ActionResult AuthenticationPass() {
            var user = _iMerchantService.GetTokenUser(UserId);
            return View(user);
        }
        /// <summary>
        /// 实名认证待审核
        /// </summary>
        /// <returns></returns>
        public ActionResult AuthenticationUnConfirm() {
            return View();
        }
        /// <summary>
        /// 实名认证未成功
        /// </summary>
        /// <returns></returns>
        public ActionResult AuthenticationNoPass() {
            var user = _iMerchantService.GetTokenUser(UserId);
            return View(user);
        }
        #endregion

        #region 购买节点商城任务
        public ActionResult OpenSupplier() {
            token_open_supplier_setting setting = _settingService.GetTokenSetting().token_open_supplier_setting;
            ViewBag.needRedDrill = setting.open_supplier_red_drill;
            ViewBag.needRMB = setting.open_supplier_rmb;
            ViewBag.Task = _iSoupTaskService.AppGetSoupTask(0, UserId, TO.ETaskType.OpenTokenSupplier);
            return View();
        }
        public ActionResult OpenSupplierPay() {
            token_open_supplier_setting setting = _settingService.GetTokenSetting().token_open_supplier_setting;
            ViewBag.needRedDrill = setting.open_supplier_red_drill;
            ViewBag.needRMB = setting.open_supplier_rmb;
            return View();
        }
        public async Task<JsonResult> OpenSupplierPost(string password) {
            var vaildResult = _iMerchantService.ValidUserPassword(UserId, password);
            if (!vaildResult)
                throw new ServerErrorException("登录密码不正确");
            var result = await _iSoupTaskService.TaskRecive(UserId, TO.ETaskType.OpenTokenSupplier);
            if (result) {
                return Json(ResultToJson.Json("领取成功", true), JsonRequestBehavior.AllowGet);
            } else {
                return Json(ResultToJson.Json("您还没有达到任务要求，无法完成任务", false), JsonRequestBehavior.AllowGet);
            }
        }
        #endregion
    }
}